layui.use(['form', 'table', 'fsoft'], function () {
  var $ = layui.$
    , form = layui.form
    , table = layui.table
    , fsoft = layui.fsoft;
  var tableId = "sysDeptTableBody";
  var searchFormId = "deptSearchForm";
  //搜索监听
  form.on('submit(sreach)', function (data) {
    mAction.doSearch(data.field);
    return false;
  });
  // 工具栏监听 （批量操作）
  var mAction = {
    doSearch: function (params) {
      //追加code、name查询条件
      if (params == undefined || params == null)
        params = $.extend({}, form.val(searchFormId));
      table.render({
        elem: '#' + tableId,
        title: '科室列表',
        where: params,
        toolbar: '#batchBtnToolbar',
        url: layui.cache['contentPath'] + '/sys-dept/findList',
        cols: [[
          { type: 'checkbox', fixed: 'left', align: 'center' }
          , { type: 'numbers', fixed: 'left', align: 'center', title: '序号' }
          , { field: 'code', title: '编码', minWidth: 100, width: 150, align: 'center', sort: true, style: "text-align:left;" }
          , { field: 'name', title: '名称', minWidth: 120, width: 180, align: 'center', sort: true, style: "text-align:left;" }
          , { field: 'location', title: '地址', minWidth: 200, width: 300, align: 'center', sort: true, style: "text-align:left;" }
          , { fixed: 'right', width: 120, align: 'center', title: '操作', toolbar: '#btnRowTool' }
        ]],
        page: { limit: 20, limits: [20, 30, 50, 80, 100], theme: '#1E9FFF' },
        height: 'full-108'
      });
    }
    , removeBatch: function (data) {
      if (data == undefined || data.length == 0) {
        layer.msg("请至少选择一条记录", { icon: 7, time: 3000 });
        return;
      }
      var ids = [];
      $.each(data, function (idx, item) {
        ids.push(item.id);
      });
      var msg = "确认要删除当前记录？";
      if (ids.length > 1) {
        msg = "确认要删除已选中的（" + ids.length + "）条记录？";
      }
      fsoft.confirm("询 问", msg, function (index) {
        fsoft.post(layui.cache['contentPath'] + '/sys-dept/remove', ids, function (rs) {
          if (rs.code == 0) {
            fsoft.successMsg("删除成功！");
            mAction.doSearch();
          } else {
            fsoft.errorMsg("删除失败:" + rs.msg);
          }
        });
        layer.close(index);
      });
    }
    , showContentForm: function (data) { //部门编辑（新增/修改）
      var path = layui.cache['contentPath'] + '/sys-dept/index/modify';
      if (data) { path += "/" + data.id }
      layer.open({
        type: 2
        , area: ['450px', '290px']
        , content: path
        , btn: ["保存", "关闭"]
        , yes: function (index) {
          var $body = layer.getChildFrame('body', index);
          var $button = $body.find('button[lay-submit]');
          $button.click();
        }
        , end: function () {
          mAction.doSearch();
        }
      });
    }
  };
  mAction.doSearch();
  table.on('toolbar(' + tableId + ')', function (obj) {
    var checkStatus = table.checkStatus(obj.config.id);
    switch (obj.event) {
      case 'add':
        mAction.showContentForm();
        break;
      case 'removeBatch':
        mAction.removeBatch(checkStatus.data);
        break;
    }
  });
  //行操作监听 
  table.on('tool(' + tableId + ')', function (obj) {
    var data = obj.data;
    if (obj.event === 'edit') {
      mAction.showContentForm(data);
    } else if (obj.event === 'remove') {
      mAction.removeBatch([data]);
    }
  }); // End of 行监听 table.on
});